﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace tools.dic
{
    public partial class fabu : Form
    {
        public fabu()
        {
            InitializeComponent();
        }

        private void txt_daimaurl_TextChanged(object sender, EventArgs e)
        {

            string dt = DateTime.Now.ToString("yyyyMMdd");

            try
            {
                //F:\项目\振大\zhenda\Web
                string yuanshiname = txt_daimaurl.Text.Trim();
                if (yuanshiname.IndexOf('\\') != -1)
                {
                    string[] shucu = yuanshiname.Split('\\');
                    txt_shuchuurl.Text += shucu[shucu.Length - 2] + "\\" + dt;

                }
            }
            catch (Exception)
            {
                txt_shuchuurl.Text += dt;
            }
        }

        private void btn_fabu_Click(object sender, EventArgs e)
        {
            try
            {
                lbl_mes.Visible = true;
                pic_load.Visible = true;
                btn_fabu.Enabled = false;
                lbl_mes.Text = "正在发布中。。。";

                Thread.Sleep(100);


                string shuchu = txt_shuchuurl.Text.Trim();

                if (shuchu.Length < 1)
                {
                    MessageBox.Show("请输入输出路径");
                    return;
                }

                if (!Directory.Exists(shuchu))
                {
                    //不存在则创建文件夹
                    Directory.CreateDirectory(shuchu);
                }
                else
                {
                    //存在则删除所有文件
                    DelectDir(shuchu);
                }

                CopyFolderTo(txt_daimaurl.Text.Trim(), shuchu);

                funfabu(shuchu);

                btn_fabu.Enabled = true;
                lbl_mes.Text = "发布成功。。。";
                pic_load.Visible = false;

                System.Diagnostics.Process.Start("explorer.exe", shuchu);

            }
            catch (Exception)
            {
                MessageBox.Show("error");
            }
        }


        void funfabu(string pt)
        {
            try
            {
                DirectoryInfo d = new DirectoryInfo(pt);
                FileSystemInfo[] fsinfos = d.GetFileSystemInfos();
                foreach (FileSystemInfo fsinfo in fsinfos)
                {
                    if (fsinfo is DirectoryInfo)     //判断是否为文件夹  
                    {
                        string yuanshiname = fsinfo.FullName;
                        if (yuanshiname.IndexOf('\\') != -1)
                        {
                            string[] shucu = yuanshiname.Split('\\');
                            if (shucu.Length > 1)
                            {
                                if (shucu[shucu.Length - 1] == "obj" || shucu[shucu.Length - 1] == ".vs" || shucu[shucu.Length - 1] == ".svn" || shucu[shucu.Length - 1] == ".git")
                                {
                                    DelectDir(yuanshiname);
                                }
                            }

                        }
                        funfabu(fsinfo.FullName);//递归调用 
                    }
                    else
                    {
                        string fullname = fsinfo.FullName;

                        string houzhui = fsinfo.Extension;

                        if (houzhui == ".cs" || houzhui == ".csproj" || houzhui == ".user" || houzhui == ".ashx.cs")
                        {
                            File.Delete(fullname);
                        }

                        //if (fullname.IndexOf(".cs") != -1 || fullname.IndexOf(".csproj") != -1 || fullname.IndexOf(".user") != -1)
                        //{
                        //    File.Delete(fullname);
                        //}
                    }
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



        void DelectDir(string srcPath)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)            //判断是否文件夹
                    {
                        DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                        subdir.Delete(true);          //删除子目录和文件
                    }
                    else
                    {
                        File.Delete(i.FullName);      //删除指定文件
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("error");
            }
        }


        /// <summary>
        /// 从一个目录将其内容复制到另一目录
        /// </summary>
        /// <param name="directorySource">源目录</param>
        /// <param name="directoryTarget">目标目录</param>
        void CopyFolderTo(string directorySource, string directoryTarget)
        {
            //检查是否存在目的目录  
            if (!Directory.Exists(directoryTarget))
            {
                Directory.CreateDirectory(directoryTarget);
            }
            //先来复制文件  
            DirectoryInfo directoryInfo = new DirectoryInfo(directorySource);
            FileInfo[] files = directoryInfo.GetFiles();
            //复制所有文件  
            foreach (FileInfo file in files)
            {
                file.CopyTo(Path.Combine(directoryTarget, file.Name));
            }
            //最后复制目录  
            DirectoryInfo[] directoryInfoArray = directoryInfo.GetDirectories();
            foreach (DirectoryInfo dir in directoryInfoArray)
            {
                CopyFolderTo(Path.Combine(directorySource, dir.Name), Path.Combine(directoryTarget, dir.Name));
            }
        }

    }
}
